#include <iostream> 
#include <vector> 
#include <cmath> 
// 基类 Shape 定义 
class Shape 
{ 
public: 
virtual double area() const = 0; // 纯虚函数，用于计算面积 
virtual ~Shape() {} // 虚析构函数，确保正确的资源释放 
}; 
// 派生类 Rectangle 定义 
class Rectangle : public Shape 
{ 
private: 
    double width, height; 
public: 
    Rectangle(double w, double h) : width(w), height(h) {} 
    double area() const override { return width * height; } 
}; 
// 派生类 Circle 定义 
class Circle : public Shape 
{ 
private: 
    double radius; public: Circle(double r) : radius(r) {} 
    double area() const override { return M_PI * radius * radius; } 
}; 
// 模板函数 calculateTotalArea 定义 
template<typename Container> 
double calculateTotalArea(const Container& shapes) 
{ 
    double totalArea = 0.0; 
    for (const auto& shape : shapes) 
    { 
        totalArea += shape->area(); 
    } return totalArea; 
} 
// 检查函数 
void check() 
{ 
    std::vector<Shape*> shapes; 
    shapes.push_back(new Rectangle(3, 4)); 
    // 矩形，面积为 20 
    shapes.push_back(new Circle(5)); 
    // 圆形，面积约为 28.2743 
    double totalArea = calculateTotalArea(shapes); 
    std::cout << "The total area of all shapes is: " << totalArea << std::endl; 
    // 清理分配的内存 
    for (auto& shape : shapes) { delete shape; } 
} 
int main() 
{ 
    check(); 
    return 0; 
}